 <template>
  <div style="padding: 10px">
<!--    功能区-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
<!--    搜索区-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="输入关键字" clearable style="width: 20%" />
      <el-button type="primary" style="margin-left: 5px" @click="search_name">查询</el-button>
    </div>
<!--    表格-->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="name" label="用户名" />
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="age" label="年龄" />
      <el-table-column prop="password" label="密码" />
      <el-table-column prop="address" label="地址" />
      <el-table-column fixed="right" label="操作" >
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
<!--    分页-->
    <div style="margin: 10px 0 ">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      />
    </div>
<!--    新增弹窗-->
    <div>
      <el-dialog v-model="dialogTableVisible" title="新增用户数据" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.name" style="width: 80%" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio v-model="form.gender" label="男">男</el-radio>
            <el-radio v-model="form.gender" label="女">女</el-radio>
            <el-radio v-model="form.gender" label="未知">未知</el-radio>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age" style="width: 80%" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" style="width: 80%" />
          </el-form-item>
          <el-form-item label="地址">
            <el-input type="textarea" v-model="form.address" style="width: 80%" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogTableVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
        </template>

      </el-dialog>
    </div>

  </div>
</template>

<script>


import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

export default {
  name: 'User',
  components: {

  },
  data() {
    return{
      form:{},
      dialogTableVisible: false,
      search:'',
      currentPage: 1,
      pageSize:10,
      total: 0,
      tableData:[

      ]
    }
  },
  created() { //页面加载 调用
    this.load()
  },
  methods: {
    search_name() {//关键字查询
      request.get("/user/search",{
        params:{
          name:this.search
        }
      }).then(res => {
        this.total = res.data.length
        this.tableData = res.data
      })
    },
    load() {
      // 分页查询
      request.get("/user/limit",{
        params:{
          startIndex:this.currentPage,
          pageSize: this.pageSize
        }
      }).then(res => {
        this.tableData = res.data
      })
      //查询总条数
      request.get("/user/select",{}).then(res => {
        this.total = res.data
      })
    },

    add() { //打开 新增数据 弹窗
      this.dialogTableVisible = true
      this.form = {}
    },
    save() {//保存数据到 后台
      if (this.form.id) {// 有id 更新数据
        request.put("/user/update", this.form).then(res => {
          if (res.code == 200) {
            ElMessage({
              message: '更新成功.',
              type: 'success',
            })
          } else {
            ElMessage.error('更新失败.')
          }
          this.load()
          this.dialogTableVisible = false
        })
      }else {//没有id 新增
        request.post("/user/save", this.form).then(res => {
          if (res.code == 200) {
            ElMessage({
              message: '新增成功.',
              type: 'success',
            })
          } else {
            ElMessage.error('新增失败.')
          }
          this.load()
          this.dialogTableVisible = false // 隐藏窗口
        })
      }
    },
    handleEdit(row) { //编辑表格信息
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogTableVisible = true
    },
    handleDelete(id) { // 删除表格数据
      request.delete("/user/delete/"+id).then(res => {
        if (res.code == 200) {
          ElMessage({
            message: '删除成功.',
            type: 'success',
          })
        } else {
          ElMessage.error('删除失败.')
        }
        this.load()//刷新表格
      })
    },
    handleSizeChange(pageSize) {//设置 每页 的 条数
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(currentPage) {// 设置 查询 页码
      this.currentPage = currentPage
      this.load()
    },
  }

}
</script>
